package com.pay.ioopos.activity;

import android.content.Intent;
import android.os.Bundle;

import com.pay.ioopos.KeyInfo;
import com.pay.ioopos.PwdListener;
import com.pay.ioopos.R;
import com.pay.ioopos.ScanListener;
import com.pay.ioopos.fragment.ComPwdValidFragment;
import com.pay.ioopos.fragment.RefundAmountFragment;
import com.pay.ioopos.fragment.RefundAmountFragment.OnAmountListner;
import com.pay.ioopos.fragment.RefundIngFragment;
import com.pay.ioopos.fragment.RefundScanFragment;
import com.pay.ioopos.fragment.TipVerticalFragment;

import static com.pay.ioopos.KeyInfo.KEY_CANCEL;
import static com.pay.ioopos.KeyInfo.KEY_ENTER;

public class RefundActivity extends AbstractActivity implements ScanListener, PwdListener, OnAmountListner {
    private String amount;
    private String code;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refund);
        showRefundPwdInput();
    }

    @Override
    public boolean onKeyDown(KeyInfo keyInfo) {
        if (mainFragment instanceof ComPwdValidFragment) {
            ComPwdValidFragment fragment = ((ComPwdValidFragment)mainFragment);
            fragment.onKeyDown(keyInfo);
        }
        if (keyInfo == KEY_CANCEL
            || (mainFragment instanceof TipVerticalFragment && keyInfo == KEY_ENTER)) {
            Intent intent = new Intent(this, StatisticsActivity.class);
            startActivity(intent);
        }
        return false;
    }

    @Override
    public void onPwdValid(String pwd) {
        showRefundAmountInput();
    }

    @Override
    public void onAmountFinish(String amount) {
        this.amount = amount;
        showRefundScan();
    }

    @Override
    public void onAmountCancel() {

    }

    @Override
    public boolean onScan(String code) {
        this.code = code;
        showRefundIng();
        return true;
    }

    private void showRefundPwdInput() {
        ComPwdValidFragment fragment = new ComPwdValidFragment();
        fragment.setPwdListener(this);
        setMainFragment(fragment);
    }

    private void showRefundAmountInput() {
        setMainFragment(new RefundAmountFragment(this));
    }

    private void showRefundScan() {
        RefundScanFragment fragment = new RefundScanFragment();
        fragment.setListener(this);
        setMainFragment(fragment);
    }

    private void showRefundIng() {
        setMainFragment(new RefundIngFragment(code, amount));
    }

}
